package com.hht.tools.baselib

import android.content.Intent
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.content.res.Resources
import android.graphics.drawable.Drawable
import androidx.core.content.ContextCompat
import com.hht.toolanduilibs.showLog
import com.hht.tools.AppContext
import com.hht.tools.R
import java.util.*

/**
 * Author:chen'hu
 * Time:2023/8/21 14:48
 * Description:This is PackageManagerUtils
 */
object PackageManagerUtils {
    private var packageManager: PackageManager = AppContext.context.packageManager

    fun getAllApkList(): ArrayList<AppBean> {
        val appNames: ArrayList<AppBean> = ArrayList();
        val intent = Intent(Intent.ACTION_MAIN, null)
        intent.addCategory(Intent.CATEGORY_LAUNCHER)
        val allApps: List<ResolveInfo> = packageManager.queryIntentActivities(intent, 0)
        Collections.sort(allApps, ResolveInfo.DisplayNameComparator(packageManager))
        for (resolveInfo in allApps) {
            val pkg = resolveInfo.activityInfo.packageName
            val name = resolveInfo.loadLabel(AppContext.context.packageManager).toString()
            val className = resolveInfo.activityInfo.name
            // var icon = resolveInfo.loadIcon(packageManager)
            val appBean = AppBean(name, pkg, className, isSystemApp(pkg, packageManager))
            appNames.add(appBean)
            "name==$name    pkg==$pkg    $className".showLog()

        }
        val appList = removeDuplicateBeans(appNames)
        "获取apps列表线程名为: ${Thread.currentThread().name}, apps个数为：${appList.size}".showLog(2)
        return appList
    }

    //根据包名，去掉重复的appBean
    private fun removeDuplicateBeans(list: ArrayList<AppBean>): ArrayList<AppBean> {
        val uniqueBeans = mutableSetOf<String>()
        val filteredList = ArrayList<AppBean>()
        list.forEach { bean ->
            if (!uniqueBeans.contains(bean.apkPkg)) {
                uniqueBeans.add(bean.apkPkg)
                filteredList.add(bean)
            }
        }
        return filteredList
    }

    private fun isSystemApp(packageName: String, packageManager: PackageManager): Boolean {
        val packageInfo = packageManager.getPackageInfo(packageName, 0)
        return (packageInfo.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0
    }

    private fun getApkName(pkg: String): String {
        return try {
            val appInfo = packageManager.getApplicationInfo(pkg, PackageManager.GET_META_DATA)
            packageManager.getApplicationLabel(appInfo).toString()
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            pkg
        }
    }

    fun getDraw(pkg: String): Drawable? {

        var drawable: Drawable? = try {
            packageManager.getApplicationIcon(pkg)
        } catch (e: PackageManager.NameNotFoundException) {
            // 处理应用程序未找到的异常
            AppContext.context.getDrawable(R.drawable.ic_launcher_background)
        } catch (e: Resources.NotFoundException) {
            AppContext.context.getDrawable(R.drawable.ic_launcher_background)
        }
        return drawable
    }
}